import {WSGameServerConnection} from "../core/WSConnection";

export class NetMgr {
<#list handlerInfos as handler>
    /** ===================== ${handler.handlerName} ===================== */
    public static ${handler.handlerName} = {
    <#list handler.apiInfos as api>
        ${api.apiName}: <#if api.returnType != "void">async</#if>(<#list api.parameters as param>${param.name}: ${toTSType(param.type)}<#if param_has_next>, </#if></#list>): <#if api.returnType != "void">Promise<${toTSType(api.returnType)}><#else>void</#if> => {
        <#if api.parameters?has_content>
            const cg = new ${api.parameterObjectName}();
            <#list api.parameters as param>
                cg.${param.name} = ${param.name};
            </#list>
            <#if api.returnType != "void">
                return await WSGameServerConnection.inst().request<${toTSType(api.returnType)}>("${api.route}", cg, "${api.parameterObjectName}", '${toTSType(api.returnType)}');
            <#else>
                WSGameServerConnection.inst().send("${api.route}", cg, "${api.parameterObjectName}");
            </#if>
        <#else>
            <#if api.returnType != "void">
                return await WSGameServerConnection.inst().request<${toTSType(api.returnType)}>("${api.route}", null, '${toTSType(api.returnType)}');
            <#else>
                WSGameServerConnection.inst().send("${api.route}", null);
            </#if>
        </#if>
        }<#if api_has_next>,</#if>
    </#list>
    };
</#list>
}

<#list protobufInfos as api>
    <#if api.parameters?has_content>
        export class ${api.className} <#if api.superClassName?has_content>extends ${api.superClassName}</#if> {
        <#list api.parameters as param>
            public ${param.name}: ${toTSType(param.type)};
        </#list>
        }
    </#if>
</#list>

export class GC {
    public code: number;
    public message: string;
    public data: Uint8Array;
}
export class Ack {
}

<#-- TypeScript 类型转换函数，支持 Java 基本类型、包装类（简写和全类名） -->
<#function toTSType csType>
    <#switch csType>
    <#-- String 类型 -->
        <#case "string">
        <#case "String">
        <#case "java.lang.String">
        <#case "long">
        <#case "Long">
        <#case "java.lang.Long">
            <#return "string">

        <#-- Number 类型（基本类型 + 包装类） -->
        <#case "int">
        <#case "Integer">
        <#case "java.lang.Integer">
        <#case "double">
        <#case "Double">
        <#case "java.lang.Double">
        <#case "float">
        <#case "Float">
        <#case "java.lang.Float">
        <#case "short">
        <#case "Short">
        <#case "java.lang.Short">
        <#case "byte">
        <#case "Byte">
        <#case "java.lang.Byte">
        <#case "java.math.BigDecimal">
        <#case "java.math.BigInteger">
            <#return "number">

        <#-- Boolean 类型 -->
        <#case "boolean">
        <#case "Boolean">
        <#case "java.lang.Boolean">
            <#return "boolean">

        <#-- Date/Time 类型 -->
        <#case "Date">
        <#case "java.util.Date">
        <#case "LocalDate">
        <#case "java.time.LocalDate">
        <#case "LocalDateTime">
        <#case "java.time.LocalDateTime">
        <#case "Instant">
        <#case "java.time.Instant">
            <#return "Date">

        <#-- 二进制/字节数组 -->
        <#case "byte[]">
        <#case "Byte[]">
        <#case "java.lang.Byte[]">
            <#return "Uint8Array">

        <#-- 默认情况（如自定义类） -->
        <#default>
            <#return csType>
    </#switch>
</#function>